<?php
namespace app\common\model\orderdata;

use think\Model;

class StorageAreaGoods extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'storageareagoods';
    protected $pk    = 'StorageAreaGoodsId';

    const STATEOK  = 1; //1为选择生效的 目前为0 是因为全部 显示
    const STATEINT = 1;
    const STATENO  = 2;//暂停

    // public function getOrderNumberAttr($value,$data)
    // {
    //     if(!empty($data['AnotherName'])){
    //         return $value.'('.$data['AnotherName'].')';
    //     }
    //     return $value;
    // }

    //涉及区域
    public function info()
    {
        return $this->hasOne('GoodS', 'GoodId', 'GoodId');
    }

    //涉及区域
    public function areainfo()
    {
        return $this->hasOne('StorageArea', 'StorageAreaId', 'StorageAreaId');
    }



    /**
     * 获取存储点列表
     * @return [type] [description]
     */
    public static function getlist($id = [])
    {
        if (empty($id)) {
            return moerr();
        }
        
        $data = StorageAreaGoods::where('StorageAreaGoodsId', 'in', $id);
        $data = $data->order('GoodId asc');
        $data = $data->with(['info']);
        $data = $data->select();
        $data->hidden(['OperationLog','info.OperationLog']);
        return $data;
    }


    /**
     * 修改货物重量
     * @param  [type] $GoodIds [description]
     * @param  [type] $weights [description]
     * @param  string $type    [add sub]
     * @return [type]          [description]
     */
    public static function weight_change($GoodIds, $weights, $type = 'add')
    {
        if (!is_array($GoodIds)) {
            if (stripos($GoodIds, ',') !== false) {
                $GoodIds = explode($GoodIds, ',');
                $weights = explode($weights, ',');
            } else {
                $GoodIds = [$GoodIds];
                $weights = [$weights];
            }
        }
        //按照货物id从新调整数顺序
        $nweights = [];
        if ($type == 'add') {
            foreach ($GoodIds as $key => $value) {
                $nweights[$value] = $weights[$key];
            }
        } else if ($type == 'sub') {
            foreach ($GoodIds as $key => $value) {
                $nweights[$value] = -1 * $weights[$key];
            }
        }
        //检测货物是否足够减少
        $sb = Storagearea::where('GoodId', 'in', $GoodIds)->select();
        if ($type == 'sub') {
            foreach ($sb as $key => $data) {
                if (((empty($data->TotalWeight) ? 0 : $data->TotalWeight) + $nweights[$data->GoodId]) < 0) {
                    return serr($data->OrderNumber . '的库存重量不足以调整');
                }
            }
        }
        //调整库存
        foreach ($sb as $key => $data) {
            if (empty($data->TotalWeight)) {
                $data->TotalWeight = $nweights[$data->GoodId];
            } else {
                $data->TotalWeight = $data->TotalWeight + $nweights[$data->GoodId];
            }
            $data->save();
        }
        return sok();
    }

	public function good()
	{
		return $this->belongsTo(Goods::class,'GoodId', 'GoodId');
    }

}
